open(BUFF,"testfile.txt");
open(WRBUFF,">testfile1.txt");
while($line=<BUFF>)
{
	chomp($line);
	@array = split(/\t/,$line);
	@array1 = split('__',$array[1]);
	
	for($i=0;$i<@array1;$i++)
	{
		if($array[1] =~ /boundry/)
		{
			@array2 = split('_',$array[1]);
			$array1[$i] = $array2[0]."\t"."boundry"."\t".$array2[2]."\t".$array2[3];
		}
		$l = $array[0]."\t".$array1[$i];
		$l =~ s/_/\t/g;
		print WRBUFF $l."\n";
	}
}
system("sort -k1,1 -k4,4 -k2,2n -k3,3n testfile1.txt > testfile2.txt");
system("cat  testfile2.txt |uniq >testfile3.txt");
system("rm testfile2.txt");
system("rm testfile1.txt");
print "step 1 done\n";
open(BUFF,"testfile3.txt");
open(WRBUFF,">testfile4.txt");
$prevgene = "";
$prevchr = "";
$bin = 1;
$kk =1;
$prevpos =-1;
while($line = <BUFF>)
{
	chomp($line);
	@arr = split("\t",$line);
	if($prevchr ne $arr[0] && $prevchr ne "")	
	{
		$bin =1;
	}
	if($prevgene ne $arr[3] && $prevgene ne "")
	{
		$bin =1;
	}
	if($arr[1] != $prevpos+1 && $prevpos != -1 && $prevgene eq $arr[3])
	{
		$bin++;
	}
	else
	{
		print "success\t$line\n";
	}
	print WRBUFF $line."\t".$bin."\n";
	$prevchr = $arr[0];
	$prevgene = $arr[3];
	$hash{$prevgene} = $bin;
	$prevpos = $arr[2];
}

while(($key,$value)=each %hash)
{
	print $key."\t".$value."\n";
}
system("rm testfile3.txt");
